package com.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.Users;
import com.crm.service.UsersService;
import com.crm.util.MD5Util;
import com.crm.util.Result;

//用户的控制层
@RestController
@RequestMapping("/users")
public class UsersController {
	@Autowired
	private UsersService usersService;// 自动注入服务层

	/**
	 * 查询所有咨询师
	 * 
	 * @param m
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getZiXun")
	public Object getZiXun(@RequestParam Map<String, Object> m, int page, int limit) {
		m.put("page", (page - 1) * limit);
		m.put("limit", limit);
		Result result = new Result("0", "true", usersService.getZiXunCount(m), usersService.getZiXun(m));
		return result;
	}
	/**
	 * 多条件查询所有用户
	 * 
	 * @param m
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getUser")
	public Object getUser(@RequestParam Map<String, Object> m, int page, int limit) {
		m.put("page", (page - 1) * limit);
		m.put("limit", limit);
		Result result = new Result("0", "true", usersService.getUserCount(m), usersService.getUser(m));
		return result;
	}

	/**
	 * 多条件查询用户和角色
	 * 
	 * @param m
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/getUserByIdRoles")
	public Object getUserById(@RequestParam Map<String, Object> m, int page, int limit) {
		m.put("page", (page - 1) * limit);
		m.put("limit", limit);
		Result result = new Result("0", "true", usersService.getUserByIdCounnt(m), usersService.getUserById(m));
		return result;
	}

	/**
	 * 添加的方法 用户名不能重复
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	public Object add(Users users, String loginName) {

		if (usersService.getByUserName(loginName) == null) {// ==null无值
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			users.setPassword(MD5Util.getMD5(users.getPassword()));
			users.setIsLockout(0);//0为锁定
			users.setPsdWrongTime(0);
			users.setWeight(0);
			users.setCrateTime(df.format(new Date()));
			int i = usersService.add(users);
			Result result = new Result("0", "true", i, "后台请求成功");
			return result;
		}
		Result result = new Result("0", "用户名已存在", 0, "success");
		return result;
	}

	/**
	 * 编辑用户 修改用户是否锁定 和被锁定的时间
	 * 
	 * @return
	 */
	@RequestMapping("/updateIsLockout")
	public Object updateIsLockout(Users users) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		users.setLockTime(df.format(new Date()));
		Result result = new Result("0", "true", usersService.updateIsLockout(users), "后台请求成功");
		return result;
	}

	/**
	 * 编辑用户 修改用户密码
	 * 
	 * @return
	 */
	@RequestMapping("/updatePwd")
	public Object updatePwd(Users users) {
		users.setPassword(MD5Util.getMD5("111111"));
		Result result = new Result("0", "true", usersService.updatePwd(users), "后台请求成功");
		return result;
	}

	/**
	 * 编辑用户
	 * 
	 * @return
	 */
	@RequestMapping("/update")
	public Object update(Users users) {
		Result result = new Result("0", "true", usersService.update(users), "后台请求成功");
		return result;
	}
	/**
	 * 根据用户id修改权重
	 * 
	 * @return
	 */
	@RequestMapping("/updateWeight")
	public Object updateWeight(Users users) {
		Result result = new Result("0", "true", usersService.updateWeight(users), "后台请求成功");
		return result;
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	@RequestMapping("/del")
	public Object del(int id, String loginName) {
		// 根据用户id，获取用户所拥有的角色信息usersService.getUserByIdRole(id);
		// 当前登录的用户不能删除 usersService.getByUserName(loginName) 根据名字去查询当前用户
		if (usersService.getByUserName(loginName) == null || usersService.getUserByIdRole(id) == 0) {// ==null不是当前用户
			Result result = new Result("0", "true", usersService.del(id), "后台请求成功");
			return result;
		}
		Result result = new Result("0", "不能删除，该用户分配有角色或是当前登录用户", 0, "后台请求成功");
		return result;
	}

}
